﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.CM.Stat
{
    /// <summary>
    /// sellmanfee 的摘要说明。
    /// </summary>
    public partial class SellManFeePage : Page
    {
        private DateTime begintime;
        private DateTime endtime;
        private int sellmanid;

        #region Protected static methods

        protected static string GetCurStatus(string curstatus)
        {
            switch (curstatus)
            {
                case "trace":
                    return ("跟踪");
                case "boot":
                    return ("启动");
                case "commend":
                    return ("产品推荐");
                case "requirement":
                    return ("需求定义");
                case "submit":
                    return ("方案提交");
                case "negotiate":
                    return ("商务谈判");
                case "actualize":
                    return ("项目实施");
                case "traceservice":
                    return ("跟踪服务");
                case "last":
                    return ("收尾款");
            }
            return ("");
        }

        protected static string GetAddMan(DataRow[] row)
        {
            if (row.Length != 0)
            {
                return (row[0]["realname"].ToString());
            }
            return ("");
        }

        protected static string GetLinkMan(DataRow[] row)
        {
            if (row.Length != 0)
            {
                return (row[0]["Name"].ToString());
            }
            return ("");
        }

        #endregion Protected static methods

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                sellmanid = (Request.QueryString["Sellmanid"] == null) ? 0 : Int32.Parse(Request.QueryString["Sellmanid"]);
                begintime = (Request.QueryString["begintime"] == null) ? DateTime.Now : DateTime.Parse(Request.QueryString["begintime"]);
                endtime = (Request.QueryString["endtime"] == null) ? DateTime.Now : DateTime.Parse(Request.QueryString["endtime"]);
                ViewState["sellmanid"] = sellmanid;
                ViewState["begintime"] = begintime;
                ViewState["endtime"] = endtime;
                BindData();
            }
            else
            {
                sellmanid = Int32.Parse(ViewState["sellmanid"].ToString());
                begintime = DateTime.Parse(ViewState["begintime"].ToString());
                endtime = DateTime.Parse(ViewState["endtime"].ToString());
            }

            base.OnLoad(e);
        }

        private void BindData()
        {
            var cm = new Components.CM();

            var ds = new DataSet();
            var staff = new Components.Staff();
            SqlDataReader dr_Staff = staff.GetAllStaffs();
            DataTable dt_Staff = Tools.ConvertDataReaderToDataTable(dr_Staff);
            dt_Staff.TableName = "Staff";
            ds.Tables.Add(dt_Staff);

            SqlDataReader dr_Linkman = cm.GetAllLinkman();
            DataTable dt_Linkman = Tools.ConvertDataReaderToDataTable(dr_Linkman);
            dt_Linkman.TableName = "Linkman";
            ds.Tables.Add(dt_Linkman);

            SqlDataReader dr_client = cm.GetContactedClientBySellmanID(sellmanid, begintime, endtime);
            DataTable dt_client = Tools.ConvertDataReaderToDataTable(dr_client);
            dt_client.TableName = "Client";
            ds.Tables.Add(dt_client);

            SqlDataReader dr_Fee = cm.GetFeeBySellmanID(sellmanid, begintime, endtime);
            DataTable dt_Fee = Tools.ConvertDataReaderToDataTable(dr_Fee);
            dt_Fee.TableName = "Fee";
            ds.Tables.Add(dt_Fee);

            ds.Relations.Add("ClientAddMan_Staff", ds.Tables["Client"].Columns["AddManID"], ds.Tables["Staff"].Columns["Staff_ID"], false);
            ds.Relations.Add("ClientLinkman_Staff", ds.Tables["Client"].Columns["ChiefLinkmanID"], ds.Tables["Linkman"].Columns["ID"], false);
            ds.Relations.Add("Client_Fee", ds.Tables["Client"].Columns["ID"], ds.Tables["Fee"].Columns["ClientID"], false);

            dgrd_fee.DataSource = dt_client.DefaultView;
            dgrd_fee.DataBind();
        }
    }
}